package restlet.android;

import com.cse210.grocerwe.client.SGroup;
import com.cse210.grocerwe.client.SItem;
import java.util.ArrayList;
import android.util.Log;

import restlet.android.BuildListActivity.GrocerWeListObserver;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.ListView;


public class GoShoppingActivity extends Activity {
	
    //private ArrayList<SGroup> m_groups = new ArrayList<SGroup>();
    private ArrayList<String> selectedGroups = new ArrayList<String>();
    private ArrayList<String> selectedGroupKeys = new ArrayList<String>();
    private ShoppingListsAdapter2 m_adapter;
	private GrocerResource resource = new GrocerResource();
    private final String user = UserCredentials.getUserName();
    private ListView listView;
    private SGroup [] myGroups = resource.getGroups(user);
    private final int SHOP_ITEMS = 0x01;
    private final String TAG = "GoShoppingActivity";
    private final boolean LIVE = true; 

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
        Log.v(TAG, "Shopping activity starting");
	    setContentView(R.layout.goshopping);
	
	    Log.v(TAG, "User in GSA: " + user);
	    
	    // Retrieve the ListView from the layout
	    listView = (ListView) findViewById(R.id.ShoppinglistView);
	    Button CancelButton = (Button) findViewById(R.id.CancelButton);
	    Button GoButton = (Button) findViewById(R.id.GoButton);
	    
        CancelButton.setOnClickListener(new OnClickListener()
        {            
            @Override
            public void onClick(View v)
            {
            	Log.v(TAG, "Canceling!");
            	finish();
            }
        });
        
        GoButton.setOnClickListener(new OnClickListener()
        {            
            @Override
            public void onClick(View v)
            {
                Log.v(TAG, "Going to shop!");
                GoShoppingActivity.this.takeListsForShopping();
            }

        });	    
	    
        m_adapter = new ShoppingListsAdapter2(GoShoppingActivity.this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>());
        listView.setAdapter(m_adapter);
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                       
	    
        // Add the groups to the list

        for(SGroup group: myGroups){ 
            m_adapter.addGroup(group.getName());
        }
        m_adapter.notifyDataSetChanged();
	}
	
	private void takeListsForShopping() {
        Log.v(TAG, "Taking lists for Shopping");
        boolean invoke = false;
        for (int i = 0; i < myGroups.length;i++)
        {
            if(listView.isItemChecked(i))
            {
            	invoke = true;
            	Log.v(TAG, "Checked item at index " + i);
            	Log.v(TAG, "Selected Group: " + myGroups[i].getName());
                selectedGroups.add(myGroups[i].getName());
                selectedGroupKeys.add(myGroups[i].getKey());
                if(LIVE)
                {
                	Log.v(TAG, "Claiming list: " + myGroups[i].getKey());
            	    resource.claimList(myGroups[i].getKey(), user);
                }
            }
        }
        
        if (invoke == true)
        {
        	Log.v(TAG, "Invoking shopping interface Activity");
            // Here we have to invoke the activity to actually buy items while shopping
            Intent myIntent = new Intent (GoShoppingActivity.this, ShoppingActivity.class);
            myIntent.putStringArrayListExtra("selected_groups_list", selectedGroups);
            myIntent.putStringArrayListExtra("selected_groupkeys_list", selectedGroupKeys);
            GoShoppingActivity.this.startActivityForResult(myIntent, SHOP_ITEMS);
        }
	}
	
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.v(TAG, "Done with shopping!!");
        finish();
    }
}
        


